<?php

namespace App\Controller\Api;

use App\Base\BaseController4Restful;
use App\Entity\Money;
use App\Entity\MoneyLog;
use App\Repository\MoneyLogTypeRepository;
use App\Repository\MoneyRepository;
use App\Repository\MoneyTypeRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class ApiMoneyController extends BaseController4Restful
{
    /**
     * @Route("/api/moneys", name="apiGetMoneys",methods={"GET","POST"})
     */
    public function apiGetMoneys(MoneyRepository $repository, Request $request)
    {
        switch ($request->getMethod()) {
            case "POST":
                $data = $this->getRequestData();
                /**@var $Money Money */
                $Money = $this->createEntity($data, Money::class);
                $this->myEm()->persist($Money);
                $this->myEm()->flush();
                return $this->json_out(200, "创建成功", $Money);
            default:
                if (!$page = $this->getParamRequired("page")) {
                    $page = 1;
                }
                $limit = 10;
                $offset = (int)abs($limit * ($page - 1));
                $total = $repository->count([]);
                $this->setMeta($page, $total, $limit);
                $list = $repository->findBy(array(), array(), $limit, $offset);
                return $this->json_out(200, "获取成功", $list);
        }
    }

    /**
     * @Route("/api/moneys/{id}", name="apiAddMoneys",methods={"GET","PUT","PATCH","DELETE"})
     */
    public function apiUpdateMoneys(Money $Money, Request $request, MoneyLogTypeRepository $moneyLogTypeRepository)
    {
        $data = $this->getRequestData();
        switch ($request->getMethod()) {
            case "DELETE":
                $this->myEm()->remove($Money);
                $this->myEm()->flush();
                return $this->json_out(200, "已移除");
            case "PATCH":
            case "PUT":
            $orgMoney = $Money->getMoney();
            /**@var $Money Money */
            $Money = $this->updateEntity($data, Money::class, $Money);
            $toMoney = $Money->getMoney();
            //添加记录
            $log = new MoneyLog();
            $log->setUser($Money->getUser())
                ->setCreateAt(new \DateTime())
                ->setMoneyType($Money->getType())
                ->setMoney($toMoney - $orgMoney)
                ->setLogType($moneyLogTypeRepository->find(3))
                ->setTips("管理员调整{$orgMoney}->{$toMoney}");
            $this->myEm()->persist($log);
            $this->myEm()->flush();
                break;
            case "Get":
            default:
        }
        $this->myEm()->persist($Money);
        $this->myEm()->flush();
        return $this->json_out(200, "获取成功", $Money);
    }


    /**
     * @Route("/api/my/money", name="ApiMyMoneys",methods={"GET"})
     */
    public function ApiMyMoneys(MoneyRepository $repository, MoneyTypeRepository $moneyTypeRepository)
    {
        $user = $this->needToBeUser();
        $moneyAll = $user->getMoney();
        $allType = $moneyTypeRepository->getAll();
        $re = [];
        foreach ($allType as $moneyType) {
            $find = 0;
            foreach ($moneyAll as $moneyOne) {
                if ($moneyOne->getType() == $moneyType) {
                    $find = 1;
                    $money = $moneyOne;
                }
            }
            if (!$find) {
                $money = new Money();
                $money->setMoney(0)
                    ->setUser($user)
                    ->setType($moneyType);
                $this->myEm()->persist($money);
                $this->myEm()->flush();
                $user->addMoney($money);
            }
            $re["m" . $money->getId()] = $money->getMoney();

        }
        $this->setGroups("app", "logType");

        return $this->json_out(200, "获取成功", $re);
    }
}
